﻿Option Explicit On

'*******************************************************************
' Program: xmlVisio
' Author: Albert E Edlund
' Date:
'
' Purpose:
' Working with visio drawing in an xml format
'
'
'
'*******************************************************************

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Text
Imports System.Text.StringBuilder

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports System.IO
Imports System.Windows.Forms
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Xml.Linq
Imports System.Xml.XPath

Imports <xmlns="http://schemas.microsoft.com/visio/2003/core">
Imports <xmlns:vx="http://schemas.microsoft.com/visio/2006/extension">
Imports <xmlns:v14="http://schemas.microsoft.com/office/visio/2010/extension">




Public Class frmDocument


#Region " Properties and Declarations "


    '
    ' where are we keeping the drawings
    '
    Private _dataPath As String
    Public Property dataPath As String
        Get
            dataPath = _dataPath
        End Get
        Set(value As String)
            _dataPath = value
        End Set
    End Property

    '
    ' what is the latest document
    '
    Public _xDoc As XDocument
    Public Property xDoc As XDocument
        Get
            Return Me._xDoc
        End Get
        Set(value As XDocument)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "xDoc is Null.")
            End If
            Me._xDoc = value
        End Set
    End Property


#End Region


    '
    ' events
    '
    Public Sub getEvents(ByRef lvControl As ListView)

        If lvControl Is Nothing Then lvControl = Me.lvEvents

        Dim qryEvents = xDoc...<EventItem>
        Dim intEvents As Integer = qryEvents.Count

        Dim lngId As ULong = 0
        Dim shtEventCode As UShort = 0
        Dim shtAction As UShort = 0
        Dim strTarget As String = ""
        Dim strTargetArgs As String = ""

        lvControl.Items.Clear()
        lvControl.Columns.Clear()
        lvControl.View = View.Details
        lvControl.AllowColumnReorder = True
        lvControl.FullRowSelect = True

        lvControl.Columns.Add("Id", 50, HorizontalAlignment.Right)

        lvControl.Columns.Add("Action", 50, HorizontalAlignment.Left)
        ' ushort
        lvControl.Columns.Add("Code", 50, HorizontalAlignment.Right)
        ' string
        lvControl.Columns.Add("Target", 200, HorizontalAlignment.Left)
        ' string
        lvControl.Columns.Add("TargArgs", 200, HorizontalAlignment.Left)


        Dim lvItem As ListViewItem

        For Each vevent In qryEvents

            Dim lngEventId As Long = -1

            Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                From att In vevent.Attributes() _
                Select att
            For Each att As XAttribute In listOfAttributes
                'Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)

                Select Case LCase(att.Name.LocalName)
                    Case "id"
                        lngId = CLng(att.Value)
                    Case "eventcode"
                        shtEventCode = CShort(att.Value)
                    Case "action"
                        shtAction = CShort(att.Value)
                    Case "target"
                        strTarget = CStr(att.Value)
                    Case "targetargs"
                        strTargetArgs = CStr(att.Value)

                End Select
            Next att

            lvItem = New ListViewItem(lngId)
            lvItem.SubItems.Add(CStr(shtEventCode))
            lvItem.SubItems.Add(CStr(shtAction))
            lvItem.SubItems.Add(strTarget)
            lvItem.SubItems.Add(strTargetArgs)


            ' add it to the listview on the data page
            lvControl.Items.Add(lvItem)

        Next vevent



    End Sub

    '
    '
    '
    Public Sub getDocProperties()

        Me.txtCategory.Text = ""
        Me.txtCreator.Text = ""
        Me.txtDescr.Text = ""
        Me.txtKeywords.Text = ""
        Me.txtSubject.Text = ""
        Me.txtTemplate.Text = ""
        Me.txtTimeCreated.Text = ""
        Me.txtTimePrinted.Text = ""
        Me.txtTimeSaved.Text = ""
        Me.txtTitle.Text = ""


        Dim qryProperties = xDoc...<DocumentProperties>

        Dim listOfAttributes As IEnumerable(Of XAttribute) = _
            From att In qryProperties.First.Attributes() _
            Select att
        For Each att As XAttribute In listOfAttributes
            Console.WriteLine("Attr {0}={1}", att.Name.LocalName, att.Value)

            Select Case LCase(att.Name.LocalName)
            End Select

        Next att

        ' start the element drilldown
        Dim listOfElements As IEnumerable(Of XElement) = _
                From elem In qryProperties.Elements() _
                Select elem
        For Each elem As XElement In listOfElements
            ' Console.WriteLine("Elem {0}={1}", elem.Name.LocalName, elem.Value)

            Select Case LCase(elem.Name.LocalName)

                Case "title"
                    txtTitle.Text = CStr(elem.Value)
                    Exit Select
                Case "subject"
                    txtSubject.Text = CStr(elem.Value)
                    Exit Select
                Case "creator"
                    txtCreator.Text = CStr(elem.Value)
                    Exit Select
                Case "manager"
                    Exit Select
                Case "company"
                    Exit Select
                Case "category"
                    txtCategory.Text = CStr(elem.Value)
                    Exit Select
                Case "keywords"
                    txtKeywords.Text = CStr(elem.Value)
                    Exit Select
                Case "desc"
                    txtDescr.Text = CStr(elem.Value)
                    Exit Select
                Case "template"
                    txtTemplate.Text = CStr(elem.Value)
                Case "timecreated"
                    txtTimeCreated.Text = CStr(elem.Value)
                    Exit Select
                Case "timesaved"
                    txtTimeSaved.Text = CStr(elem.Value)
                    Exit Select
                Case "timeedited"
                    Exit Select
                Case "timeprinted"
                    txtTimePrinted.Text = CStr(elem.Value)
                Case "previewpicture"

            End Select

        Next elem



    End Sub

    '
    '
    '
    Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        frmAbout.ShowDialog()
    End Sub

    '
    '
    '
    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

End Class